<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>标签滑动</title>
    <style>
        *{margin: 0;padding: 0;}
        body{background-color: #f4f4f4;padding-top: 100px;}
        .inline{display: inline-block;}
        .container{width: 300px;height: 40px;padding: 0 40px;margin: 0 auto;position: relative;box-shadow: 0px 1px 2px 0 rgba(0,0,0,.1);}
        .container *{font-size: 14px;}
        .prev,.next{position:absolute;width: 40px;height: 40px;line-height: 40px;text-align: center;background-color:#FFFFFF;cursor: pointer;}
        .prev{left: 0;box-shadow: 1px 0 2px 0 rgba(0,0,0,.1);z-index: 99;}
        .next{right: 0;box-shadow: -1px 0 2px 0 rgba(0,0,0,.1);z-index: 99;}
        .tab-container{overflow: hidden;background-color: #FFFFFF;}
        #mobile-tag{position: relative;left:0;transition: all .3s ease-out;list-style: none;white-space: nowrap;font-size: 0;}
        #mobile-tag li{display:inline-block;font-size: 14px;height: 40px;line-height: 40px;padding: 0 8px;border-right: 1px solid #f4f4f4;box-sizing: border-box;position:relative}
    </style>
</head>
<body>
    <div class="container">
        <div class="prev">前页</div>
        <div class="next">下页</div>
        <div class="tab-container">
            <ul id="mobile-tag">
                <li>标签一</li>
                <li>标签二</li>
                <li>标签三</li>
                <li>标签四</li>
                <li>标签五</li>
                <li>胜多负少</li>
                <li>是的冯绍峰</li>
                <li>几节课</li>
                <li>看见门口</li>
                <li>OK破</li>
                <li>奇偶看</li>
                <li>梦见啥了快递费</li>
                <li>柯尼卡</li>
            </ul>
        </div>
    </div>
    <script src="/dom/dom.js"></script>
    <script>
        
        let tagMove = function(containerEl,direct){
            let left = parseFloat(Dom.css(containerEl,'left'))
            ,containerWidth = containerEl.offsetWidth
            ,lis = [...containerEl.querySelectorAll('li')];
            if(direct == 'left'){
                if(!left && left==0){return}
                //left绝对值减去容器宽度
                let preTabLeft = -left - containerWidth
                for(let i=0,len=lis.length; i<len; i++){
                    let rect = Dom.position(lis[i]);
                    if(rect.left >= preTabLeft){
                        Dom.css(containerEl,'left',-rect.left+'px');
                        return;
                    }
                }
            }else{
                for(let i=0,len=lis.length; i<len; i++){
                    let rect = Dom.position(lis[i]);
                    if(rect.left + rect.width - Math.abs(left) >= containerWidth){
                        Dom.css(containerEl,'left',-rect.left+'px');
                        return;
                    }
                }
            }
        };
        let containerEl = Dom.getEl('#mobile-tag')
        Dom.evtListener('.next','click',function(){
            tagMove(containerEl)
        })
        Dom.evtListener('.prev','click',function(){
            tagMove(containerEl,'left')
        })
    </script>
</body>
</html>